package com.noahsloan.nutils.collections;

import java.util.List;
import java.util.ListIterator;

/**
 * Creates an iterator that travels a list backwards instead of forwards.
 * 
 * @author noah
 * 
 * @param <E>
 */
public class ReverseIterator<E> implements ListIterator<E> {

	private final ListIterator<E> it;

	public ReverseIterator(List<E> list) {
		// get an iterator starting at the end
		this.it = list.listIterator(list.size());
	}

	public boolean hasNext() {
		return it.hasPrevious();
	}

	public E next() {
		return it.previous();
	}

	public void remove() {
		it.remove();
	}

	public void add(E o) {
		it.add(o);
	}

	public boolean hasPrevious() {
		return it.hasNext();
	}

	public int nextIndex() {
		return it.previousIndex();
	}

	public E previous() {
		return it.next();
	}

	public int previousIndex() {
		return it.nextIndex();
	}

	public void set(E o) {
		it.set(o);
	}

}
